package cn.xscrum.vtsp.service.impl;

import cn.xscrum.common.core.text.Convert;
import cn.xscrum.common.exception.BusinessException;
import cn.xscrum.common.utils.DateUtils;
import cn.xscrum.common.utils.StringUtils;
import cn.xscrum.framework.util.ShiroUtils;
import cn.xscrum.vtsp.domain.VtspBlacklist;
import cn.xscrum.vtsp.domain.VtspVolunteerManage;
import cn.xscrum.vtsp.mapper.VtspBlacklistMapper;
import cn.xscrum.vtsp.mapper.VtspVolunteerManageMapper;
import cn.xscrum.vtsp.service.IVtspMessageService;
import cn.xscrum.vtsp.service.IVtspVolunteerManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 志愿者管理信息Service业务层处理
 *
 * @author liuwl
 * @date 2021-10-01
 */
@Service
public class VtspVolunteerManageServiceImpl implements IVtspVolunteerManageService
{
    @Autowired
    private VtspVolunteerManageMapper vtspVolunteerManageMapper;  //定义Mapper接口对象

    @Autowired
    private VtspBlacklistMapper vtspBlacklistMapper;  //定义Mapper接口对象

    @Autowired
    private IVtspMessageService vtspMessageService;

    /**
     * 查询志愿者管理ID
     *
     * @param volunteerId 志愿者信息ID
     * @return 志愿者信息
     */
    @Override
    public VtspVolunteerManage selectVtspVolunteerManageById(Long volunteerId)
    {
        return vtspVolunteerManageMapper.selectVtspVolunteerManageById(volunteerId);
    }

    /**
     * 查询志愿者证件号码
     *
     * @param cardNo 志愿者证件号码
     * @return 志愿者管理信息
     */
    public VtspVolunteerManage selectVtspVolunteerManageByCardNo(String cardNo)
    {
        return vtspVolunteerManageMapper.selectVtspVolunteerManageByCardNo(cardNo);
    }

    /**
     * 查询志愿者管理列表
     *
     * @param vtspVolunteerManage 志愿者信息
     * @return 志愿者信息
     */
    @Override
    public List<VtspVolunteerManage> selectVtspVolunteerManageList(VtspVolunteerManage vtspVolunteerManage)
    {
        return vtspVolunteerManageMapper.selectVtspVolunteerManageList(vtspVolunteerManage);
    }

    /**
     * 新增志愿者管理
     *
     * @param vtspVolunteerManage 志愿者信息
     * @return 新增结果
     */
    @Override
    public int insertVtspVolunteerManage(VtspVolunteerManage vtspVolunteerManage)
    {
        //获取创建人名称
        vtspVolunteerManage.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        //获取创建时间
        vtspVolunteerManage.setCreateTime(DateUtils.getNowDate());
        //新增志愿者信息
        int row = vtspVolunteerManageMapper.insertVtspVolunteerManage(vtspVolunteerManage);
        //插入志愿者信息为发起点--更新注册情况   状态6，userId-页面申请的连接id（对注册志愿者进行更新）
        if (row > 0){
            vtspMessageService.sendMessage(6,null,7L);
        }
        return row;
    }

    /**
     * 修改志愿者管理
     *
     * @param vtspVolunteerManage 志愿者信息
     * @return 修改结果
     */
    @Override
    public int updateVtspVolunteerManage(VtspVolunteerManage vtspVolunteerManage)
    {
        //获取更改人名称
        vtspVolunteerManage.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        //获取更改时间
        vtspVolunteerManage.setUpdateTime(DateUtils.getNowDate());
        //修改志愿者信息
        int row = vtspVolunteerManageMapper.updateVtspVolunteerManage(vtspVolunteerManage);
        //更新志愿者信息为发起点--更新学历、年龄情况   状态6，userId-页面申请的连接id（对各学历、各年龄志愿者进行更新）
        if (row > 0){
            vtspMessageService.sendMessage(6,null,7L);
        }
        return row;
    }

    /**
     * 批量删除志愿者管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 批量删除结果
     */
    @Override
    public int deleteVtspVolunteerManageByIds(String ids)
    {
        return vtspVolunteerManageMapper.deleteVtspVolunteerManageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除志愿者管理
     *
     * @param volunteerId 志愿者信息ID
     * @return 删除结果
     */
    @Override
    public int deleteVtspVolunteerManageById(Long volunteerId)
    {
        return vtspVolunteerManageMapper.deleteVtspVolunteerManageById(volunteerId);
    }

    /**
     * 导入志愿者管理文件
     *
     * @param volunteerList 志愿者信息列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importList(List<VtspVolunteerManage> volunteerList, Boolean updateSupport)
    {
        //判空
        if (StringUtils.isNull(volunteerList) || volunteerList.size() == 0)
        {
            throw new BusinessException("导入数据不能为空！");
        }
        int successNum = 0;     //导入成功数量
        int failureNum = 0;     //导入失败数量
        StringBuilder successMsg = new StringBuilder();     //导入成功响应
        StringBuilder failureMsg = new StringBuilder();     //导入失败响应

        //对导入的数据进行循环判断
        for (VtspVolunteerManage vtspVolunteerManage : volunteerList)
        {
            //导入成功
            try
            {
                //通过证件号码验证是否存在这个用户
                VtspVolunteerManage volunteerManage = vtspVolunteerManageMapper.selectVtspVolunteerManageByCardNo(vtspVolunteerManage.getCardNo());
                VtspBlacklist vtspBlacklist = vtspBlacklistMapper.selectVtspBlacklistByCardNo(vtspVolunteerManage.getCardNo());
                //用户在志愿者和黑名单志愿列表中都不存在
                if (StringUtils.isNull(volunteerManage) && StringUtils.isNull(vtspBlacklist))
                {
                    //插入
                    this.insertVtspVolunteerManage(vtspVolunteerManage);
                    successNum++;
                    //导入成功提示
                    successMsg.append("<br/>" + successNum + "志愿者信息导入成功！");
                }
                //用户存在，则更新
                else if (updateSupport)
                {
                    //更新
                    this.updateVtspVolunteerManage(vtspVolunteerManage);
                    successNum++;
                    //导入成功提示
                    successMsg.append("<br/>" + successNum + "志愿者信息更新成功！");
                }
                //信息存在
                else
                {
                    failureNum++;
                    //导入失败提示
                    failureMsg.append("<br/>" + failureNum + "志愿者信息已存在！");
                }
            }
            //导入失败
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "志愿者信息导入失败！";
                failureMsg.append(msg + e.getMessage());
            }
        }
        //结果反馈
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            //导入志愿者发起点，对通过志愿者执行发送消息到可视化工具    状态6，userId-页面申请的连接id（对正式志愿者及其他进行更新）
            vtspMessageService.sendMessage(6,null,7L);
        }
        return successMsg.toString();
    }

    /**
     * 移入黑名单 修改志愿者状态为黑名单志愿者9
     *
     * @param volunteerId 志愿者信息ID
     * @return 结果
     */
    @Override
    public int moveVtspVolunteerManageById(Long volunteerId)
    {
        return vtspVolunteerManageMapper.moveVtspVolunteerManageById(volunteerId);
    }

    /**
     * 查询志愿者服务记录详情列表
     *
     * @param volunteerId 志愿者信息ID
     * @return 志愿者服务记录详情信息
     */
    public List<VtspVolunteerManage> selectServeList(Long volunteerId)
    {
        return vtspVolunteerManageMapper.selectServeList(volunteerId);
    }

    /**
     * 查询志愿者报名记录详情列表
     *
     * @param volunteerId 志愿者信息ID
     * @return 志愿者报名记录详情信息
     */
    public List<VtspVolunteerManage> selectEnrollList(Long volunteerId)
    {
        return vtspVolunteerManageMapper.selectEnrollList(volunteerId);
    }
}
